package hlhj.fhp.checkpoint.atys

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.ShapeDrawable
import android.os.Handler
import android.os.Message
import android.text.TextUtils
import android.widget.Toast
import hlhj.fhp.checkpoint.BaseAty
import hlhj.fhp.checkpoint.R
import hlhj.fhp.checkpoint.iview.LoginView
import hlhj.fhp.checkpoint.javaBean.LoginBean
import hlhj.fhp.checkpoint.javaBean.MyBaseBean
import hlhj.fhp.checkpoint.presenter.LoginPresenter
import hlhj.fhp.checkpoint.utils.SPHelper
import hlhj.fhp.checkpoint.utils.ToastUtils
import kotlinx.android.synthetic.main.aty_login.*
import android.hardware.usb.UsbDevice.getDeviceId
import android.telephony.TelephonyManager
import android.view.View
import android.view.WindowManager
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.HttpHeaders
import com.tbruyelle.rxpermissions.RxPermissions
import hlhj.fhp.checkpoint.javaBean.SendSMSBean
import hlhj.fhp.checkpoint.utils.FormatUtil


/**
 * Created by Never Fear   on 2018\11\13 0013.
Never More....
 */
class LoginAty :BaseAty<LoginView,LoginPresenter>(), LoginView {
    private var sec=60

    private val handler= @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            sec--
            if (sec==0){
                sec=60
                btYzm.setText("验证码")
                val shapeDrawable = btYzm.background as GradientDrawable
                shapeDrawable.setColor(Color.parseColor("#FF1290F2"))
                btYzm.background=shapeDrawable
                btYzm.isClickable=true

            }else{
                btYzm.text="${sec}s"
                val shapeDrawable = btYzm.background as GradientDrawable
                shapeDrawable.setColor(Color.parseColor("#FFC3C3C3"))
                btYzm.background=shapeDrawable
                btYzm.isClickable=false
                this.sendEmptyMessageDelayed(1,1000)
            }
        }
    }

    override fun sendSmsResp(myBaseBean: SendSMSBean) {
        handler.sendEmptyMessage(1)
        if (myBaseBean.info!=null&&myBaseBean.info.isIs_show==true){
            etTuiJian.visibility=View.VISIBLE
            dv2.visibility=View.VISIBLE
        }
    }

    override fun loginResp(loginBean: LoginBean) {
        SPHelper.saveToken(this,loginBean.info.access_token)
        val httpHeaders = HttpHeaders()
        httpHeaders.put("Authorization","Bearer ${SPHelper.getToken(this)}")
        OkGo.getInstance().addCommonHeaders(httpHeaders)
        val intent = Intent(this, HomeAty::class.java)
        startActivity(intent)
        finish()
    }

    override fun initView() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
    }
    override fun logicStart() {
    }

    override fun onDestroy() {
        super.onDestroy()
        val shapeDrawable = btYzm.background as GradientDrawable
        shapeDrawable.setColor(Color.parseColor("#FF1290F2"))
        handler.removeMessages(1)
    }
    @SuppressLint("MissingPermission")
    override fun setListener() {
        btYzm.setOnClickListener {
            if (TextUtils.isEmpty(etPhone.text.toString())){
                tvError.text="手机号不能为空"
                return@setOnClickListener
            }
            if (!FormatUtil.isMobileNO(etPhone.text.toString())){
                tvError.text="手机号格式不正确"
                return@setOnClickListener
            }
            presenter?.sendSms(etPhone.text.toString(),this@LoginAty)
        }

        btGoLogin.setOnClickListener {
            if (!RxPermissions.getInstance(this).isGranted(Manifest.permission.READ_PHONE_STATE)){
                RxPermissions.getInstance(this).request(Manifest.permission.READ_PHONE_STATE,Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        .subscribe {
                            b->
                            if (b){
                                val TelephonyMgr = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                                val szImei = TelephonyMgr.deviceId
                                presenter?.login(etPhone.text.toString(),tvYzm.text.toString(),etTuiJian.text.toString(),szImei,this@LoginAty)
                            }else{
                                ToastUtils.toast("您已拒绝权限",this@LoginAty)
                            }
                        }
            }else{
                val TelephonyMgr = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
                val szImei = TelephonyMgr.deviceId
                presenter?.login(etPhone.text.toString(),tvYzm.text.toString(),etTuiJian.text.toString(),szImei,this@LoginAty)
            }

        }
    }

    override fun bindView(): LoginView {
        return this
    }

    override fun createPresenter(): LoginPresenter {
        return LoginPresenter()
    }

    override val contentId: Int= R.layout.aty_login
}